﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcWinesSample.DAL.Models;

namespace MvcWinesSample.DAL.InMemory
{
    public class MemoryRepository<TEntity>:IRepository<TEntity>
        where TEntity:ModelBase
    {

        Dictionary<int,TEntity> _dataSource=new Dictionary<int,TEntity>();
        public IQueryable<TEntity> FindAll()
        {
            return _dataSource.Values.AsQueryable();
        }

        public TEntity Get(int id)
        {
            return _dataSource[id];
        }

        public TEntity Save(TEntity entity)
        {
            if (entity.Id <= 0)
            {
                entity.Id = _dataSource.Keys.Max() + 1;
                _dataSource.Add(entity.Id,entity);
            }
            else
                _dataSource[entity.Id] = entity;

            return entity;
        }

        public void Delete(TEntity entity)
        {
            _dataSource.Remove(entity.Id);
        }

    }
}